package com.paf.android.display;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;

import com.paf.android.R;
//import flex2.tools.oem.Application;
//import flex2.tools.oem.Configuration;


import android.content.Context;
import android.os.Environment;
import android.util.Log;
import android.view.Display;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.MenuItem.OnMenuItemClickListener;
import android.view.View;
import android.view.WindowManager;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.PopupMenu;
import android.widget.Toast;

public class CustomToolbar extends LinearLayout {
	//Buttons
	public CustomButton new_button;
	public CustomButton open_button;
	public CustomButton save_button;
	public CustomButton import_button;
	public CustomButton export_button;
	public CustomButton cut_button;
	public CustomButton copy_button;
	public CustomButton paste_button;
	public CustomButton share_button;
	public CustomButton settings_button;
	public CustomButton run_button;
	public CustomButton find_button;
	public CustomButton indent_button;
	public EditText codeView;
	
	public CustomToolbar(Context context, int _id) {
		super(context);
		this.setBackgroundResource(R.drawable.empty_button);
		
		new_button = new CustomButton(context, 201, R.drawable.new_icon, this, showPopup);
		open_button = new CustomButton(context, 202, R.drawable.open_icon, this, showPopup);
		save_button = new CustomButton(context, 203, R.drawable.save_icon, this, showPopup);
		
		import_button = new CustomButton(context, 204, R.drawable.import_icon, this, showPopup);
		export_button = new CustomButton(context, 205, R.drawable.export_icon, this, exportProject);
		
		cut_button = new CustomButton(context, 206, R.drawable.cut_icon, this, showPopup);
		copy_button = new CustomButton(context, 207, R.drawable.copy_icon, this, showPopup);
		paste_button = new CustomButton(context, 208, R.drawable.paste_icon, this, showPopup);
		
		run_button = new CustomButton(context, 211, R.drawable.run_icon, this, showPopup);
		find_button = new CustomButton(context, 212, R.drawable.find_icon, this, showPopup);
		indent_button = new CustomButton(context, 213, R.drawable.indent_icon, this, showPopup);
		
		share_button = new CustomButton(context, 209, R.drawable.share_icon, this, showPopup);
		LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(android.view.ViewGroup.LayoutParams.WRAP_CONTENT, android.view.ViewGroup.LayoutParams.WRAP_CONTENT);
		WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
		Display display = wm.getDefaultDisplay();
		int margin = display.getWidth()-(11*32);
		Log.v("APP - ", "margin : "+margin);
		Log.v("APP - ", "width : "+display.getWidth());
		lp.setMargins(margin, 0, 0, 0);
		share_button.setLayoutParams(lp);
		
		settings_button = new CustomButton(context, 210, R.drawable.settings_icon, this, showPopup);
		
		
	}
	
	//Handlers
	public OnClickListener exportProject = new OnClickListener() {		
		@Override
		public void onClick(View v) {
			//Compile baby !!!
			//As3Compiler comp = new As3Compiler(As3Configuration.getDefine());
			
			/*try{
				File actionScriptFile = new File("src", "test.as");
				File outputFile = new File(Environment.getExternalStorageDirectory()+"src", "test.swf");
				Application application = new Application(actionScriptFile);
				
		    	Configuration config = application.getDefaultConfiguration();
		    	config.useActionScript3(true);
		    	config.optimize(true);
		    	
		    	application.setConfiguration(config);
		    	application.setOutput(outputFile);
		    	
		    	long result = application.build(true);
		    	if(result>0){
		    		Toast.makeText(getContext(), "Compiled OK !", Toast.LENGTH_SHORT).show();
		    	}else{
		    		Toast.makeText(getContext(), "Compiled FAILED !", Toast.LENGTH_SHORT).show();
		    	}
			}catch(IOException e){
				e.printStackTrace();
		        String importError = e.getMessage();
		        Toast.makeText(getContext(), "Error : "+importError, Toast.LENGTH_SHORT).show();
			}*/
		}
	};
	
	//showPopup() will show appropriate menu to a clicked button
    public OnClickListener showPopup = new OnClickListener() {		
		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub
			PopupMenu popup = new PopupMenu(v.getContext(), v);
	        MenuInflater inflater = popup.getMenuInflater();

	        switch(v.getId()){
	        case 201://New
	        	inflater.inflate(R.menu.new_menu, popup.getMenu());
	        	popup.show();
	        	break;
	        case 202://Open
	        	inflater.inflate(R.menu.open_menu, popup.getMenu());
	        	popup.show();
	        	break;
	        case 203://Save
	        	inflater.inflate(R.menu.save_menu, popup.getMenu());
	        	popup.show();
	        	
	        	popup.getMenu().getItem(1).setOnMenuItemClickListener(saveFile);
	        	break;
	        case 204://Import
	        	inflater.inflate(R.menu.import_menu, popup.getMenu());
	        	popup.show();
	        	break;
	        case 205://Export
	        	inflater.inflate(R.menu.export_menu, popup.getMenu());
	        	popup.show();
	        	break;
	        case 209://Share
	        	inflater.inflate(R.menu.share_menu, popup.getMenu());
	        	popup.show();
	        	break;
	        case 210://Settings
	        	inflater.inflate(R.menu.settings_menu, popup.getMenu());
	        	popup.show();
	        	break;
	        case 212://Find
	        	inflater.inflate(R.menu.find_menu, popup.getMenu());
	        	popup.show();
	        	break;
	        }
		}
		
		private String getCode() {
			String sBody=codeView.getText().toString();
			
			return sBody;
		}
		
		//Save File
		public OnMenuItemClickListener saveFile = new OnMenuItemClickListener() {
			@Override
			public boolean onMenuItemClick(MenuItem item) {
				// TODO Auto-generated method stub
				   try
				    {
				        File root = new File(Environment.getExternalStorageDirectory(), "src");
				        if (!root.exists()) {
				            root.mkdirs();
				        }
				        File gpxfile = new File(root, "test.as");
				        FileWriter writer = new FileWriter(gpxfile);
				        writer.write(getCode());
				        writer.flush();
				        writer.close();
				        Toast.makeText(getContext(), "Saved", Toast.LENGTH_SHORT).show();
				    }
				    catch(IOException e)
				    {
				         e.printStackTrace();
				         String importError = e.getMessage();
				         Toast.makeText(getContext(), "Error : "+importError, Toast.LENGTH_SHORT).show();
				    }
				   return false;
			}
		};
	};
}
